package com.example.njut;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class RegisterActivity extends AppCompatActivity {

    private EditText username;
    private EditText pwd;
    private EditText age;
    private Button register;
    private Button back;

    dbHelper dbHelper;
    String DB_Name="mydb";
    SQLiteDatabase db;
    Cursor cursor;

    boolean flag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        username = (EditText) findViewById(R.id.editUsername);
        pwd = (EditText) findViewById(R.id.editPassword);
        age = (EditText) findViewById(R.id.editText4);
        register = (Button) findViewById(R.id.button7);
        back = (Button) findViewById(R.id.button8);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RegisterActivity.this.finish();
                Intent intent = new Intent();
                intent.setClass(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
            }
        });
        //创建连接，并打开数据库
        dbHelper = new dbHelper(this,DB_Name,null,1);
        db = dbHelper.getWritableDatabase();

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ContentValues values = new ContentValues();
                cursor = db.query(dbHelper.TB_Name,null,null,null,null,null,"uid ASC");
                cursor.moveToFirst();
                while(!cursor.isAfterLast()){
                    if(username.getText().toString().trim().equals(cursor.getString(1))){
                        flag = false;
                    }
                    cursor.moveToNext();
                }
                if(username.getText().toString().trim().equals("admin")){
                    flag = false;
                }
                if(flag==true){
                    values.put("uname",username.getText().toString().trim());
                    values.put("pwd",pwd.getText().toString().trim());
                    values.put("age",age.getText().toString().trim());
                    long rowId = db.insert(dbHelper.TB_Name,null,values);
                    if(rowId==-1){
                        Toast.makeText(RegisterActivity.this,"发生未知错误，注册失败",Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(RegisterActivity.this,"注册成功！",Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent();
                        intent.setClass(RegisterActivity.this,LoginActivity.class);
                        startActivity(intent);
                        RegisterActivity.this.finish();
                    }
                }else {
                    Toast.makeText(RegisterActivity.this,"用户名已存在！",Toast.LENGTH_SHORT).show();
                    flag = true;
                }
            }
        });
    }
}
